<?php 
namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;

class Comment extends Controller
{
	/**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $data = db('comment as c, s75_user as u, s75_data as d')->field('c.id, u.name as uname, d.name as dname, content, c.ctime, cmt, gid')->where('d.id = c.cid')->where('uid = u.id')->select();
        $name =  db('comment as c, s75_user as u')->field('u.name,gid')->where('u.id = gid')->select();

        foreach ($data as $k => $v) {
            $data[$k]['gname'] = '';
            foreach ($name as $ko => $vo) {
                if ($v['gid']==$vo['gid']) {
                    $data[$k]['gname'] = $vo['name'];
                }
            }
            $data[$k] =array_diff_key($data[$k], ['gid' => '1']);
        }

        return view('comment/list', ['title'=>'评论列表','data'=>$data]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {

        return view('book/add', ['title'=>'添加图书']);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {   
         // 新增数据
        $data = $request->post();
        $res = db('data')->insert($data);
        if($res){ return $this->success('添加成功',url('admin/Book/index')); }else{ return $this->error('添加失败'); }
        
        
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    { 

    }


    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $data = db('comment as c, s75_user as u, s75_data as d')->field('c.id, u.name as uname, d.name as dname, content, c.ctime, cmt, gid')->where('d.id = c.cid')->where('uid = u.id')->where('c.id',$id)->find();
        $name =  db('comment as c, s75_user as u')->field('u.name')->where('u.id = gid')->where('c.id',$id)->find();

        return view('comment/edit', ['title'=>'编辑图书','data'=>$data, 'name'=>$name]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {   
        $data = $request->put();
        $data =array_diff_key($data, ['_method' => "Put"]);
        $res = db('data')->update($data);

        if($res){ return $this->success('修改成功',url('admin/Book/index')); }else{ return $this->error('修改失败'); }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {   
        $result = db('comment')->delete($id);

        if ($result) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为'.$id.'的图书删除成功!';
        }else{
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = '删除失败';
        }

        // echo json_encode($result);
        return json($info);
    }




}


 ?>